#!/usr/bin/perl
#
# Tidy up the persons listing before the bycountry gets at it:
# - delete unknown entries from the states and countries
# - map all missing state and city info to zzzz
# - insert counters at all levels
# The intent is that this is then sorted +2, and reported.

use POSIX;
use Reportsub;

$allentries = 9999999; # 9 million
$maxcount = $allentries;

while ($ARGV[0] =~ /^-/) {
    $opt = shift;
    if ($opt eq "-max") {
        $maxcount = shift;
    }
}

$sortitlast = "zzzz";

print STDERR "Loading country info\n";
getcountryinfo();
loadstates();

print STDERR "Reading records\n";
while (<>) {
    last if ($skip + $count > $maxcount);
    chop;
    ($key, $country, $state, $city, $name, $email, $maypublish) = split("\t");
# At the moment, I want the table to show ALL of them....
#    if ($maypublish ne "YES") {
#        #print STDERR "Skip - $maypublish\n";
#        ++ $skip;
#        next;
#    }
    # Some other reasons to skip records
    if (!$name) {
        #print STDERR "$key: No name\n";
	++ $skip;
        next;
    }
    if (!$longname{$country}) {
        print STDERR "$key: No country name for country ",
        $country, ", move to XW\n";
	$name .= " ($country)";
	$country = "XW";
    }
    $city = $sortitlast if !$city;
    if ($hasstates{$country}) {
        if ($state && !$statename{$country, $state}) {
            #print STDERR "$key: Unknown state ",
            #$state, " in ", $country, "\n";
            #$state = "";
	    ++ $unknownstates{$country}{$state};
	    $state = "Unknown state";
        }
        if (!$state) {
            $state = $sortitlast;
        }
    } else {
        $state = "";
    }
    ++ $count;
    print "$key\t$country\t$state\t$city\t$name\t$email\t$maypublish\n";
}
print STDERR "$count records counted, $skip records skipped\n";
print STDERR "Unknown states found\n";
for $country (keys(%unknownstates)) {
    print STDERR "In $country $longname{$country}:\n";
    for $state (keys(%{$unknownstates{$country}})) {
	print STDERR "  $unknownstates{$country}{$state} $state\n";
    }
}

